package com.example.parking.controller;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.parking.dto.Result;
import com.example.parking.dto.UserFrom;
import com.example.parking.entity.User;
import com.example.parking.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/user")
@CrossOrigin//配置跨域
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 分页查询全部用户信息
     * @return
     */
    @GetMapping("/getUser")
    public Result getUser(@RequestParam Integer pageNum,
                          @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        Page<User> page = new Page<>(pageNum,pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("state",1);
        Page<User> userPage = userService.page(page, wrapper);
        return Result.successData(userPage);
    }

    /**
     * 分页模糊查询
     * @param username
     * @return
     */
    @GetMapping("/getByName")
    public Result getByName(@RequestParam(required = false,defaultValue = "1")Integer pageNum,
                                @RequestParam(required = false,defaultValue = "10")Integer pageSize,
                                @Param("username") String username){
        //配置分页规则
        Page<User> pageInfo = new Page<>(pageNum,pageSize);
        //进行参数筛选
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(username),User::getUsername,username);
        wrapper.eq(User::getState,1);
        Page<User> userInfo = userService.page(pageInfo, wrapper);
        return Result.successData(userInfo);
    }

    /**
     * 根据id删除用户(管理员操作Database)
     * @param userId
     */
    @PostMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable Integer userId){
        userService.removeById(userId);
        return Result.success();
    }

    /**
     * 按需修改用户信息(管理员修改)
     * @param userFrom
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserFrom userFrom){
        User user = BeanUtil.toBean(userFrom,User.class);
        if(userService.updateById(user)){
            return Result.success();
        }else{
            return Result.fail("更新失败");
        }
    }

    /**
     * 获取当前登录用户的信息
     * @param userId
     * @return
     */
    @GetMapping("/getById/{userId}")
    public Result getById(@PathVariable Integer userId){
        User user = userService.getById(userId);
        UserFrom userFrom = BeanUtil.copyProperties(user, UserFrom.class);
        return Result.successData(userFrom);
    }

}
